<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
/*内门禁分组管理*/
class SlidingGroup extends AdminBase{
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();  
    }
	/*内门禁列表*/
	function index(){
		echo $this->fetch();
	}	
	//删除内门禁分组
	function delete(){
		$id=input("id");
		db("SlidingGroup")->where("id =$id")->delete();
		$this->ajax_success("成功");
	}
	function edit(){
		$model=db("SlidingGroup")->find(input('id'));
		$this->assign("list",db("SlidingDoor")->select());
		$this->assign("model",$model);
		$this->assign("door_ids",explode(",", $model['door_ids']));
		echo $this->fetch();
	}
	function edit_post(){
		if(request()->isPost()){
			if(empty($_POST['door_ids'])){
				$this->ajax_error("请选择内门禁");
			}else{
				$door_ids=implode(",", $_POST['door_ids']);
				$list=db("SlidingDoor")->where("id in($door_ids)")->column("code");
				$pvalue=0;
				if(!empty($list)){
					foreach ($list as $key => $value) {
						$pvalue+=hexdec($value);
					}
				}
				$pvalue=dechex($pvalue);
				db("SlidingGroup")->update(array("id"=>input("id"),"pname"=>input("pname"),"pdesc"=>input("pdesc"),"pvalue"=>$pvalue,"door_ids"=>",".$door_ids.","));
				$this->ajax_success("成功");
			}
		}
	}
	
	/*卡列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	/*分配权限*/
	function give(){
		$model=db("SlidingGroup")->find(input('id'));
		$this->assign("model",$model);
		echo $this->fetch();
	}
	
	function give_post(){
		if(request()->isPost()){
			$model=db("Interface")->where("card_no='".input("card_no","xxx")."'")->setField(array("sliding_group_id"=>input("sliding_group_id")));
			$this->ajax_success("成功");	
		}
	}
	function add(){
		$this->assign("list",db("SlidingDoor")->select());
		echo $this->fetch();
	}
	function add_post(){
		if(request()->isPost()){
			if(empty($_POST['door_ids'])){
				$this->ajax_error("请选择内门禁");
			}else{
				$door_ids=implode(",", $_POST['door_ids']);
				$list=db("SlidingDoor")->where("id in($door_ids)")->column("code");
				$pvalue=0;
				if(!empty($list)){
					foreach ($list as $key => $value) {
						$pvalue+=hexdec($value);
					}
				}
				$pvalue=dechex($pvalue);
				db("SlidingGroup")->insert(array("pname"=>input("pname"),"pdesc"=>input("pdesc"),"pvalue"=>$pvalue,"door_ids"=>",".$door_ids.","));
				$this->ajax_success("成功");
			}
		}
	}
	function query(){
		if(request()->isGet()){
			echo $this->fetch();
		}else{
		$pvalue=preg_replace("/^0*/", "",input("pvalue","xxxx"));
		$model=db("SlidingGroup")->where("pvalue='$pvalue'")->find();
		if(!empty($model['door_ids'])){
				$doors=db("SlidingDoor")->where("id in( 0 $model[door_ids] 0) ")->fetchSql(FALSE)->column("name");
				if(!empty($doors)){
					$model['detail']=implode(",", $doors);	
				}
		}else{
			$model['detail']="";
		}
			$this->ajax_success("成功",$model);
		}
	}
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10041006");
		$this->assign("menus",$auth_group);
	}
	
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
		 'pname'=>array("field" =>"pname","operator" =>"like"),
		 );
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$total=db("SlidingGroup")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("SlidingGroup")->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		foreach ($list as $key => $value) {
			if(!empty($value)){
				$doors=db("SlidingDoor")->where("id in( 0 $value[door_ids] 0) ")->fetchSql(FALSE)->column("name");
				if(!empty($doors)){
					$list[$key]['detail']=implode(",", $doors);	
				}
			}else{
				$list[$key]['detail']="";
			}
		}
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
	
}
